/// SET DIRECTORY TO THE FOLDER "replication files"
 
 import spss using "Encuesta CEP 84 Dic 2019 v1.sav", clear
 


/////////////////////////
/// 
/// CREATE VARIABLES for regressions

* complex design variable 

egen float strata_cep = group(REGION ZONA)

* tipology

gen tipo=1 if ESP_33==1
replace tipo=2 if ESP_33==2
replace tipo=3 if ESP_33>2 & ESP_32<3
replace tipo=4 if ESP_32==5 | ESP_32==4
replace tipo=5 if ESP_32>6 |ESP_32==3


label define ltipo 1 "Strong protester" 2 "Weak protester" 3 "Supporter" ///
						4 "Opponent" 5 "Undecided"
label values tipo ltipo


* Political characterization

gen sexo=DS_P1
gen edad=DS_P2_EXACTA

gen educ = 1 if DS_P4<4
replace educ = 2 if DS_P4==4 | DS_P4==5 | DS_P4==7
replace educ = 3 if DS_P4==6 | DS_P4==8 | DS_P4==9


label define leduc 1 "Uncompleted secondary" 2 "Completed secondary" /// 
					3 "Completed tertiary"
label values educ leduc

gen noid=[DS_P20>10]
gen left=[DS_P20>=1 & DS_P20<=4]

gen incumb_support=(MB_P7==1)

gen voted_incumbent_2nd=(DS_P23==2)
replace voted_incumbent_2nd=. if DS_P23>2

gen int_pol_quite_very=(MB_P14<3)

gen marcha=(MB_P12_1<4) 

gen turned_out=(DS_P21==1)

* trust

forvalues var =1/17 {
	gen conf`var' = [ MB_P11_`var'>=1 &  MB_P11_`var'<=2]
}


* Inequality

  gen e38_desig=0
 replace e38_desig=1 if ESP_38A==1
 replace e38_desig=1 if ESP_38B==1
 

 gen mip_desig=0
 replace mip_desig=1 if MB_P1_1==16
 replace mip_desig=1 if MB_P1_2==16
 replace mip_desig=1 if MB_P1_3==16
 
 
gen equality_vs_individual=ESP_11 if ESP_11<11

gen resp_gov_vs_ind=ESP_12  if ESP_12<11

gen meritocracy=ESP_13 if ESP_13<11

gen recomp=(ESP_17_3>=1 & ESP_17_3<=2)


gen rico_educ = 0
replace rico_educ = 1 if ESP_10_1 == 6 | ESP_10_2 == 6

gen rico_resp = 0
replace rico_resp = 1 if ESP_10_1 == 4 | ESP_10_2 == 4

gen rico_ini=0
replace rico_ini = 1 if ESP_10_1 == 1 | ESP_10_2 == 1

gen rico_net = 0
replace rico_net = 1 if ESP_10_1 == 5 | ESP_10_2 == 5

gen rico_padres = 0
replace rico_padres = 1 if ESP_10_1 == 7 | ESP_10_2 == 7

gen favricos=(ESP_17_1>=1 & ESP_17_1<=2)

gen popubciu=[ESP_5_1==1]
replace popubciu=1 if ESP_5_1==2

gen popubown=[ESP_5_2==1]
replace popubown=1 if ESP_5_2==2

gen popub_ricos=[ESP_5_3==1]
replace popub_ricos=1 if ESP_5_3==2

*** earned vs shoud earn

 gen gana_ob= ESP_14C1/100 if ESP_14C1>99
  gen should_ob= ESP_15C1/100  if ESP_15C1>99
 
  gen gana_gte= ESP_14B1/1000  if ESP_14B1>99
  gen should_gte= ESP_15B1/1000  if ESP_15B1>99
  
  
   gen gana_doc= ESP_14A1/1000  if ESP_14A1>99
  gen should_doc= ESP_15A1/1000  if ESP_15A1>99
  
  
** Attitudes towards democracy


forvalues var =1/4 {
	generat ESP_49_`var'r = 6 - ESP_49_`var' if ESP_49_`var' < 6
}

alpha ESP_49_1r - ESP_49_4r, item gen(auto)

recode ESP_4 (1/4=1 "lib") (5/6=2 "centro") (7/10=3 "orden") (88/99=.), gen(libseg)
gen lib=[libseg==1]
gen ord=[libseg==3]

gen dempref=[ESP_1==1]
gen evalmal=[ ESP_2>=4 &  ESP_2<=5]


gen d_evad=[ESP_41_2>=1 & ESP_41_2<=2]
gen d_barric=[ESP_41_3>=1 & ESP_41_3<=2]
gen d_incend=[ESP_41_4>=1 & ESP_41_4<=2]
gen d_saqueo=[ESP_41_5>=1 & ESP_41_5<=2]


gen d_e42_1=[ESP_42_1>=1 & ESP_42_1<=2]
gen d_e42_2=[ESP_42_2>=1 & ESP_42_2<=2]

gen violddhh=0
replace violddhh=1 if ESP_43>=1 & ESP_43<=2
replace violddhh=1 if ESP_44>=1 & ESP_44<=2


/// Additional variables used for Figures


  recode ESP_1 (1 = 1 "DemPref" ) (2=2 "AlgAut") /// 
 (3=3 "DaLoMismo") (8/9 = 4 "nsnc" ), generate (e1)
 
    recode ESP_2 (1/2 = 1 "MB/B" ) (3= 2 "REG") /// 
(4/5 = 3 "MM/M") (8/9 = 4 "ns/nc"), generate (e2)


 recode ESP_41_2 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e41_evad)

recode ESP_41_3 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e41_barric)

recode ESP_41_4 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e41_incend)

recode ESP_41_5 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e41_saqueo)


recode ESP_42_1 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e42_1)


recode ESP_42_2 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e42_2)

 recode ESP_43 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e43)


recode ESP_44 (1/2 = 1 "Siemp/casi" ) (3= 2 "Aveces") /// 
(4/5 = 3 "Nunca/casi") (88/99 = 4 "ns/nc"), generate (e44)

recode DS_P2_EXACTA (18/24= 1 "18-24") (25/34 = 2 "25-34") ///
(35/44 = 3 "35-44") (45/54= 4 "45-54") (55/max= 5 "55 o más"), generate (EDAD_T)



gen higher_ed=(educ==3)
gen no_high_school=(educ==1)



save "cep84.dta", replace
